package com.leyou.item.web;

import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author: wangzibeng
 * Date: 2019/4/28 0028
 * Time: 12:42
 */

@RestController
@RequestMapping("category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("of/parent")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByPid(@RequestParam(value = "pid", defaultValue = "0") Long pid) {
        return ResponseEntity.ok(categoryService.queryCategoryByPid(pid));
    }

    @PostMapping("add")
    public ResponseEntity<Long> addCategory(@RequestBody CategoryDTO categoryDTO) {
        return ResponseEntity.ok(categoryService.addCategory(categoryDTO));
    }


    @PutMapping("edit")
    public ResponseEntity<Void> editCategoryById(@RequestBody CategoryDTO categoryDTO) {
        categoryService.editCategoryById(categoryDTO.getId(), categoryDTO.getName());
        return ResponseEntity.status(HttpStatus.OK).build();
    }

    @DeleteMapping("delete/{id}")
    public ResponseEntity<Void> deleteCategoryById(@PathVariable("id") Long id) {
        categoryService.deleteCategory(id);
        return ResponseEntity.status(HttpStatus.OK).build();
    }

    /**
     * @param id
     * @return
     */
    @GetMapping("of/brand")
    public ResponseEntity<List<CategoryDTO>> getCategoryByBrand(@RequestParam("id") Long id) {
        return ResponseEntity.ok(categoryService.getCategoryByBrand(id));
    }

    /**
     * 根据id的集合查询商品分类
     *
     * @param idList 商品分类的id集合
     * @return category 集合
     */
    @GetMapping("list")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByIds(@RequestParam("ids") List<Long> idList) {
        return ResponseEntity.ok(categoryService.queryCategoryByIds(idList));
    }

    @GetMapping("levels")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByCid3(@RequestParam("cid3") Long cid3) {
        return ResponseEntity.ok(categoryService.queryCategoryByCid3(cid3));
    }

}
